﻿using Kwan.Data;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System;
using BG.Web.Admin;

namespace BG.Web
{
    public static class CrudManage
    {
        public static void DeleteSelected(string obj, string tableName)
        {
            string[] list = obj.Split(',');
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Length; ++i)
            {
                if (i == 0)
                {
                    sb.Append("DELETE FROM ");
                    sb.Append(tableName);
                    sb.Append(" WHERE");
                }
                else
                    sb.Append(" OR");
                sb.Append(" EntityID=");
                sb.Append(list[i]);
            }
            using (IConnectinContext cc = DBContext.Context1)
            {
                Command cmd = new Command(sb.ToString());
                cc.ExecuteReader(cmd);
            }
        }

        public static void ChangeSelected(string obj, string NameValue, string tableName)
        {
            if (obj.Length > 0)
            {
                string[] list = obj.Split(',');
                string[] nv = NameValue.Split(char.Parse("_"));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < list.Length; ++i)
                {
                    sb.Append("UPDATE ");
                    sb.Append(tableName);
                    sb.Append(" SET ");
                    sb.Append(nv[0]);
                    sb.Append(" = ");
                    sb.Append(nv[1]);
                    sb.Append(" WHERE EntityID=");
                    sb.Append(list[i]);
                    sb.Append(";");
                }
                using (IConnectinContext cc = DBContext.Context1)
                {
                    Command cmd = new Command(sb.ToString());
                    cc.ExecuteReader(cmd);
                }
            }
            else
            {
                ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
            
        }

        public static void ChangeState<T>(IList<T> list, string NameValue)
            where T : Kwan.Data.Mappings.DataObject, new()
        {
                foreach (T tt in list)
                {
                    string[] arr = NameValue.Split(char.Parse("_"));
                    SetPropertyValue<T>(tt, arr[0], arr[1]);
                    tt.EntityState._Loaded = true;
                    DBContext.Save(tt);
                }
        }

        public static T SetPropertyValue<T>(T t, string key, string value)
             where T : Kwan.Data.Mappings.DataObject, new()
        {
            PropertyInfo[] pi = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (PropertyInfo item in pi)
            {
                if (key.Contains(item.Name))
                {
                    item.SetValue(t, Convert.ChangeType(value, item.PropertyType, System.Globalization.CultureInfo.CurrentCulture), null);
                    break;
                }
            }
            return t;
        }
    }
}